from django.http import JsonResponse

from comment.comservice import add_filter
from comment.comviews import com_views, success_response, err_response, format_response
from utils import meansureutils
from utils.dbutils import DB_Config, MysqlTools
from utils.formatutils import get_params
from utils.pageutils import paging_simple
from .sqlmap import *
from utils.jpushutils import j_push
from utils.rbmqutils import RabbitPublisher

mysql_tools = MysqlTools(db_config=DB_Config.get("default"))

table = {}
second_name = {}  # 走rabbitmq需要用到的名字
CONFIG_ID_NAME = mysql_tools.select_all(ProcessSqlMap.CONFIG_ID_NAME)
for list in CONFIG_ID_NAME:
    if list["process_name"] == "请假申请":
        table[list["config_id"]] = "aoa_holiday"
        second_name[list["config_id"]] = "holiday"
    elif list["process_name"] == "离职申请":
        table[list["config_id"]] = "aoa_resign"
        second_name[list["config_id"]] = "resign"
    elif list["process_name"] == "外出申请":
        table[list["config_id"]] = "aoa_go_out"
        second_name[list["config_id"]] = "goOut"
    elif list["process_name"] == "费用报销":
        table[list["config_id"]] = "aoa_bursement"
        second_name[list["config_id"]] = "burse"
    elif list["process_name"] == "采购申请":
        table[list["config_id"]] = "aoa_purchase"
        second_name[list["config_id"]] = "purchase"
    elif list["process_name"] == "临停放行":
        table[list["config_id"]] = "aoa_free_stop"
        second_name[list["config_id"]] = "freeStop"
    elif list["process_name"] == "特殊车辆":
        table[list["config_id"]] = "aoa_special_car"
        second_name[list["config_id"]] = "specialCar"
    elif list["process_name"] == "加班申请":
        table[list["config_id"]] = "aoa_overtime"
        second_name[list["config_id"]] = "overTime"
    elif list["process_name"] == "出差申请":
        table[list["config_id"]] = "aoa_evection"
        second_name[list["config_id"]] = "evection"
    elif list["process_name"] == "转正申请":
        table[list["config_id"]] = "aoa_regular"
        second_name[list["config_id"]] = "regular"
    elif list["process_name"] == "借款申请":
        table[list["config_id"]] = "aoa_borrow"
        second_name[list["config_id"]] = "borrow"
    elif list["process_name"] == "付款申请":
        table[list["config_id"]] = "aoa_payment"
        second_name[list["config_id"]] = "payment"
    elif list["process_name"] == "公章申请":
        table[list["config_id"]] = "aoa_chapter"
        second_name[list["config_id"]] = "chapter"
    elif list["process_name"] == "合同会签":
        table[list["config_id"]] = "aoa_contract"
        second_name[list["config_id"]] = "contract"
    elif list["process_name"] == "特殊事项":
        table[list["config_id"]] = "aoa_process_list"
        second_name[list["config_id"]] = "specialMatter"


# table = {
#     1: "aoa_holiday",
#     2: "aoa_resign",
#     3: "aoa_go_out",
#     4: "aoa_bursement",
#     5: "aoa_purchase",
#     6: "aoa_free_stop",
#     7: "aoa_special_car"
# }

# second_name = {
#     1: "holiday",
#     2: resign",
#     3: "goOut",
#     4: "burse",
#     5: "purchase",
#     6: "freeStop",
#     7: "specialCar"
# }


def my_apply(params):
    '''
    我发起的流程
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    # if not params["user_id"]:
    #     return err_response(210)
    sql = add_filter(ProcessSqlMap.my_apply, params, ["query_str"],
                     **ProcessSqlMap.FILTER_PARAMS)
    sql += " order by t1.apply_time desc"
    result = mysql_tools.select_all(sql.format(**params))
    if isinstance(result, JsonResponse):
        return result
    if params.get("isall") != 1:
        result = paging_simple(result, params.get("page"), params.get("size"))
    if not result:
        return success_response([])
    for res in result:
        res["apply_time"] = str(res["apply_time"])
        if res["status_name"] == "已结束":
            res["step"] = "已结束"
        elif res["status_name"] == "未通过":
            res["step"] = "未通过"
        else:
            res["step"] = res.get("shenuserzhi")
        res.pop("shenuserzhi")
    return success_response(result)


def my_unreviewed(params):
    '''
    待我审核的流程
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    my = mysql_tools.select_one(ProcessSqlMap.check_user_name.format(**params))
    params["dept_id"] = my.get("dept_id")
    params["position_name"] = my.get("position_name")
    sql_result = add_filter(ProcessSqlMap.my_unreviewed, params, ["query_str"], **ProcessSqlMap.FILTER_PARAMS)
    sql_result += " order by t1.apply_time desc"
    all_process = mysql_tools.select_all(sql_result.format(**params))
    if isinstance(all_process, JsonResponse):
        return all_process
    result = []
    for process in all_process:
        if process.get("shenuserzhi") == "部门经理审核":
            if process.get("dept_id") == my.get("dept_id"):
                result.append(process)
        else:
            result.append(process)
    
    if isinstance(result, JsonResponse):
        return result
    if not result:
        return success_response([])
    if params.get("isall") != 1:
        result = paging_simple(result, page=params.get("page"), size=params.get("size"))
    for res in result:
        res["apply_time"] = str(res.get("apply_time"))
        if res["status_name"] == "已结束":
            res["step"] = "已结束"
        elif res["status_name"] == "未通过":
            res["step"] = "未通过"
        else:
            res["step"] = res.get("shenuserzhi")
        res.pop("shenuserzhi")
    return success_response(result)


# def my_unreviewed(params):
#     '''
#     待我审核的流程
#     :param params:
#     :return:
#     '''
#     dict = {
#         "user_id": "user_id"
#     }
#     page_params = {
#         "isall": "isAll",
#         "size": "limit",
#         "page": "page",
#         "query_str": "query_str"
#     }
#     params = get_params(params, dict, page_params)
#     if isinstance(params, JsonResponse):
#         return params
#     sql = add_filter(ProcessSqlMap.my_unreviewed, params, ["query_str"],
#                      **ProcessSqlMap.FILTER_PARAMS)
#     sql += " order by t1.apply_time desc"
#     result = mysql_tools.select_all(sql.format(**params))
#     if params.get("isall") != 1:
#         if isinstance(result,JsonResponse):
#             return result
#         result = paging_simple(result, params.get("page"), params.get("size"))
#     if not result:
#         return success_response([])
#     for res in result:
#         res["apply_time"] = str(res["apply_time"])
#         if res["status_name"] == "已结束":
#             res["step"] = "已通过"
#         elif res["status_name"] == "未通过":
#             res["step"] = "未通过"
#         else:
#             res["step"] = res["shenuserzhi"] + "审批"
#         res.pop("shenuserzhi")
#     return success_response(result)


def my_reviewed(params):
    '''
    我已审批的
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    sql = add_filter(ProcessSqlMap.my_reviewed, params, ["query_str"],
                     **ProcessSqlMap.FILTER_PARAMS)
    sql += " order by t1.apply_time desc"
    result = mysql_tools.select_all(sql.format(**params))
    if not result:
        return success_response([])
    if params.get("isall") != 1:
        result = paging_simple(result, params.get("page"), params.get("size"))
    for res in result:
        res["apply_time"] = str(res["apply_time"])
        if res["status_name"] == "已结束":
            res["step"] = "已结束"
        elif res["status_name"] == "未通过":
            res["step"] = "未通过"
        else:
            res["step"] = res.get("shenuserzhi")
        res.pop("shenuserzhi")
    return success_response(result)


def check_process(params):
    '''
    查询流程
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id",
        "process_id": "process_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    sql_type_name = ProcessSqlMap.check_table.format(**params)
    type_user = mysql_tools.select_one(sql_type_name)
    if isinstance(type_user, JsonResponse):
        return type_user
    table_id = type_user.get("config_id")
    if not table_id:
        return err_response(210)
    # 创建人的姓名和部门
    sql_user = ProcessSqlMap.check_user.format(**{"process_user_id": type_user["process_user_id"]})
    process_user = mysql_tools.select_one(sql_user)
    params["table_name"] = table.get(table_id)
    if params["table_name"] == "aoa_holiday":
        sql = ProcessSqlMap.check_process.format(**params)
        result = mysql_tools.select_one(sql)
    elif params["table_name"] == "aoa_process_list":
        result = {}
    else:
        sql = ProcessSqlMap.check_process_not_type_id.format(**params)
        result = mysql_tools.select_one(sql)
    if params["table_name"] not in ["aoa_free_stop", "aoa_special_car", "aoa_resign", "aoa_borrow", "aoa_regular",
                                    "aoa_payment", "aoa_chapter", "aoa_contract", "aoa_process_list"]:
        result.pop("type_id")
    result["apply_time"] = str(type_user["apply_time"])
    result["deeply_name"] = type_user["deeply_name"]
    result["process_name"] = type_user["process_name"]
    result["process_des"] = type_user["process_des"]
    result["real_name"] = process_user["real_name"]
    result["dept_name"] = process_user["dept_name"]
    result["config_id"] = table_id
    # result.pop("pro_id")
    # if params["table_name"] not in ["aoa_borrow", "aoa_payment"]:
    #     result.pop("manager_advice")
    if "manager_advice" in result.keys():
        result.pop("manager_advice")
    if "personnel_advice" in result.keys():
        result.pop("personnel_advice")
    result["attach_id"] = type_user["attach_id"]
    result["attachment_name"] = type_user["attachment_name"]
    if params["table_name"] not in ["aoa_bursement", "aoa_purchase", "aoa_process_list"]:
        pri_name = mysql_tools.select_one(ProcessSqlMap.check_prikey.format(**params))
        result.pop(pri_name.get("column_name"))
    reviewed_res = mysql_tools.select_all(ProcessSqlMap.check_reviewed.format(**params))
    list = []
    for res in reviewed_res:
        res["reviewed_time"] = str(res["reviewed_time"])
        if res["status_name"] == "已结束":
            res["status_name"] = "已批准"
        list.append(res)
    result["reviewed"] = list
    # if params.get("isall") is not True:
    #     result = paging_simple(result, params.get("page"), params.get("size"))
    if type_user.get("start_time"):
        result["start_time"] = str(type_user.get("start_time"))
    if type_user.get("end_time"):
        result["end_time"] = str(type_user.get("end_time"))
    
    if params["table_name"] == "aoa_holiday":
        # result.pop("holiday_id")
        result["leave_days"] = str(result["leave_days"]) + "天"
    elif params["table_name"] == "aoa_go_out":
        # result.pop("go_out_id")
        result.pop("leave_days")
        # result.pop("type_name")
        result["leave_time"] = str(result["leave_time"]) + "小时"
    elif params["table_name"] == "aoa_resign":
        # result.pop("resign_id")
        result["is_finish"] = int.from_bytes(result["is_finish"], byteorder='big')
        sql_resign = ProcessSqlMap.check_user_name.format(**{"user_id": result["hand_user"]})
        resign_name = mysql_tools.select_one(sql_resign)
        result["hand_user_name"] = resign_name.get("real_name")
        result.pop("hand_user")
        result.pop("suggest")
        result.pop("financial_advice")
        if result.get("is_finish") == 0:
            result["is_finish"] = "费用报销已完成"
        else:
            result["is_finish"] = "费用报销未完成"
    elif params["table_name"] == "aoa_bursement":
        sql = ProcessSqlMap.check_deatilsburse.format(**{"bursement_id": result.get('bursement_id')})
        bursedetails = mysql_tools.select_all(sql)
        for details in bursedetails:
            details["produce_time"] = str(details["produce_time"])
        result["bursedetails"] = bursedetails
        result.pop("user_name")
        result.pop("operation_name")
        # result.pop("bursement_id")
        result.pop("burse_time")
        result.pop("financial_advice")
        result["capitalize"] = meansureutils.rmb_to_number(result["all_money"])  # capitalize(result["all_money"])
    elif params["table_name"] == "aoa_purchase":
        sql = ProcessSqlMap.check_deatilspurch.format(**{"purchase_id": result["purchase_id"]})
        purchasedetails = mysql_tools.select_all(sql)
        result["purchasedetails"] = purchasedetails
        # result.pop("purchase_id")
        result.pop("financial_advice")
        result.pop("operation_name")
        result.pop("allinvoices")
        result.pop("burse_time")
        # result.pop("type_name")
        # result["burse_time"] = str(result["burse_time"])
    elif params["table_name"] == "aoa_free_stop":
        # result.pop("free_stop_id")
        result["consume_money"] = str(result["consume_money"]) + "元"  # 坑,double整数不显示.0
        result["derate_money"] = str(result["derate_money"]) + "元"
        sql_derate_level_name = ProcessSqlMap.check_derate_level.format(**result)
        derate_level_name = mysql_tools.select_one(sql_derate_level_name)
        result["derate_level"] = derate_level_name["type_name"]
    
    elif params["table_name"] == "aoa_special_car":
        # result.pop("special_car_id")
        sql_derate_level_name = ProcessSqlMap.check_derate_level.format(**result)
        derate_level_name = mysql_tools.select_one(sql_derate_level_name)
        result["derate_level"] = derate_level_name["type_name"]
    
    elif params["table_name"] == "aoa_overtime":
        result["time"] = str(result.get("time")) + "小时"
    
    elif params["table_name"] == "aoa_evection":
        result["leave_days"] = str(result.get("leave_days")) + "天"
        sql_type_name = ProcessSqlMap.check_type.format(**{"type_id": result.get("traffic")})
        type_name = mysql_tools.select_one(sql_type_name)
        result["type_name"] = type_name.get("type_name")
        result.pop("traffic")
    
    elif params["table_name"] == "aoa_regular":
        result.pop("deficiency")
        result.pop("pullulate")
        result.pop("understand")
        result.pop("days")
    
    elif params["table_name"] == "aoa_borrow":
        result["employ_time"] = str(result.get("employ_time"))
        result["money"] = str(result.get("money")) + "元"
        sql_type_name = ProcessSqlMap.check_type.format(**{"type_id": result.get("pay_type")})
        type_name = mysql_tools.select_one(sql_type_name)
        result["type_name"] = type_name.get("type_name")
    
    elif params["table_name"] == "aoa_payment":
        result["finally_time"] = str(result.get("end_time"))
        result.pop("end_time")
        result["money"] = str(result.get("money")) + "元"
        sql_type_name = ProcessSqlMap.check_type.format(**{"type_id": result.get("pay_type")})
        type_name = mysql_tools.select_one(sql_type_name)
        result["type_name"] = type_name.get("type_name")
    
    elif params["table_name"] == "aoa_chapter":
        sql_type_name = ProcessSqlMap.check_type.format(**{"type_id": result.get("chapter_type")})
        type_name = mysql_tools.select_one(sql_type_name)
        result["type_name"] = type_name.get("type_name")
    
    elif params["table_name"] == "aoa_contract":
        result["money"] = str(result.get("money")) + "元"
    
    elif params["table_name"] == "aoa_special_matter":
        pass
    return success_response(result)


def examine_process(params):
    '''
    审批流程
    '''
    dict = {
        "user_id": "user_id",
        "process_id": "process_id",
        "status_id": "status_id",
        "advice": "advice"
    }
    params = get_params(params, dict)
    if isinstance(params, JsonResponse):
        return params
    sql_type_name = ProcessSqlMap.check_table.format(**params)
    type_user = mysql_tools.select_one(sql_type_name)
    table_name = table.get(type_user.get("config_id"))
    java_name = second_name.get(type_user.get("config_id"))
    if table_name == "aoa_process_list":
        pri_id = params.get("process_id")
    else:
        params["table_name"] = table_name
        pri_name = mysql_tools.select_one(ProcessSqlMap.check_prikey.format(**params))
        params["pri_name"] = pri_name.get("column_name")  # 主键名称
        pri_id = mysql_tools.select_one(ProcessSqlMap.check_second_id.format(**params)).get("pri_name")
    
    push_dict = {
        # "userId": type_user.get("process_user_id"),     # 流程申请人
        "userId": params.get("user_id"),  # 流程审核人
        "processId": params.get("process_id"),  # process_list的id
        # "classId": pri_id,                              # 流程各自单独的表的主键id
        # "processName": java_name,                       # 流程的别名
        "statusId": params.get("status_id"),  # 审核状态
        "advice": params.get("advice"),  # 审核意见
        "isStart": False,  # 是否是发起
    }
    RabbitPublisher.run("submitprocess", push_dict)
    return success_response()


# def examine_process(params):
#     '''
#     审批流程
#     :param params:
#     :return:
#     '''
#     dict = {
#         "user_id": "user_id",
#         "process_id": "process_id",
#         "status_id": "status_id",
#         "advice": "advice"
#     }
#     # null_params = {
#     #     "next_user_id": "next_user_id"
#     # }
#     # params = get_params(params, dict, null_params)
#     params = get_params(params, dict)
#     if isinstance(params, JsonResponse):
#         return params
#     status_name = mysql_tools.select_one(ProcessSqlMap.check_status_name.format(**params))
#     params["status_name"] = status_name.get("status_name")
#     sql_type_name = ProcessSqlMap.check_table.format(**params)
#     type_user = mysql_tools.select_one(sql_type_name)
#     table_name = table.get(type_user.get("type_name"))
#     name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))  # 判断当前用户职位和名字
#     params["real_name"] = name_position.get("real_name")
#     params["name"] = name_position.get("name")
#     params["table_name"] = table_name
#     if params["status_name"] == "未通过":
#         params["is_checked"] = 1
#         mysql_tools.execute_sql(ProcessSqlMap.update_reviewed.format(**params))
#         mysql_tools.execute_sql(ProcessSqlMap.update_process_list.format(**params))
#         sql = ProcessSqlMap.update_table.format(**params)
#         mysql_tools.execute_sql(sql, commit=True)
#         # message = "你的%s%s" % (table_name, status_name["status_name"])
#         # j_push.delay(message, [type_user.get("user_name")])
#
#     elif params["status_name"] == "已批准":
#         # if not params["next_user_id"]:
#         #     params["status_name"] = "已结束"
#         #     params["is_checked"] = 0
#         #     id = mysql_tools.select_one(ProcessSqlMap.check_status_id.format(**params))
#         #     params["status_id"] = id["status_id"]
#         #     mysql_tools.execute_sql(ProcessSqlMap.update_reviewed.format(**params))
#         #     mysql_tools.execute_sql(ProcessSqlMap.update_process_list.format(**params))
#         #     sql = ProcessSqlMap.update_table.format(**params)
#         #     mysql_tools.execute_sql(sql, commit=True)
#             # message = "你的%s%s" % (table_name, "已通过")
#             # j_push.delay(message, [type_user.get("user_name")])
#
#         # else:
#             # if params.get("next_user_id"):
#                 # message = "收到一个待审批流程"
#                 # next_user_name = mysql_tools.select_one(ProcessSqlMap.check_user_name.format(**{"user_id": params.get("next_user_id")}))
#                 # j_push.delay(message, [next_user_name.get("user_name")])
#             mysql_tools.execute_sql(ProcessSqlMap.update_reviewed.format(**params))
#             mysql_tools.execute_sql(ProcessSqlMap.update_process_list_new.format(**params))
#             sql = ProcessSqlMap.update_table.format(**params)
#             mysql_tools.execute_sql(sql)
#             # params["statsu_name"] = "未处理"
#             # id = mysql_tools.select_one(ProcessSqlMap.check_status_id.format(**params))
#             # params["next_status_id"] = id["status_id"]
#             # mysql_tools.execute_sql(ProcessSqlMap.create_reviewed_examine.format(**params), commit=True)
#             params["table_name"] = table.get(type_user.get("config_id"))
#             pri_name = mysql_tools.select_one(ProcessSqlMap.check_prikey.format(**params))
#             params["pri_name"] = pri_name.get("column_name")    # 主键名称
#             pri_id = mysql_tools.select_one(ProcessSqlMap.check_second_id.format(**params))
#             process.delay(type_user.get("process_user_id"), params.get("process_id"), pri_id.get("pri_name"), pri_name.get("column_name"))
#     else:
#         return err_response(210)
#     return success_response()


def create_holiday(params):
    '''
    创建请假流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "leave_time": "leave_days",
        "holiday_id": "holiday_id"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if key == "attach_id":
    #         continue
    #     if val is None:
    #         return err_response(210)
    # if not params.get("attach_id"):
    # params["attach_id"] = "null"
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("user_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    # check_holiday_name = mysql_tools.select_one(ProcessSqlMap.check_holiday_name.format(**params))
    # params["holiday_name"] = check_holiday_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_id = ProcessSqlMap.check_type_id.format(**params)
    # type_id = mysql_tools.select_one(sql_id)
    # params["type_id"] = type_id["type_id"]
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    sql = ProcessSqlMap.create_holiday.format(**params)
    mysql_tools.execute_sql(sql)
    holiday_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": holiday_id,
            "processName": "holiday",
            "isStart": True,  # 是否是发起
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, holiday_id, "holiday")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_go_out(params):
    '''
    创建外出流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "address": "address",
        "leave_time": "leave_time"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if val is None:
    #         return err_response(210)
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    sql = ProcessSqlMap.create_go_out.format(**params)
    mysql_tools.execute_sql(sql)
    go_out_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": go_out_id,
            "processName": "goOut"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, go_out_id, "goOut")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_resign(params):
    '''
    创建离职流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "is_finish": "is_finish",
        "nofinish": "nofinish",
        "hand_user": "hand_user"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if val is None:
    #         return err_response(210)
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    params["leave_time"] = "null"
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    sql = ProcessSqlMap.create_resign.format(**params)
    mysql_tools.execute_sql(sql)
    resign_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": resign_id,
            "processName": "resign"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, resign_id, "resign")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_bursement(params):
    '''
    创建报销流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "name": "name",
    }
    null_params = {
        "attach_id": "attach_id",
        "bursedetails": "bursedetails"
    }
    replace_params = {
        "attach_id": "null",
        "bursedetails": []
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if key == "attach_id":
    #         continue
    #     if val is None:
    #         return err_response(210)
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    params["leave_time"] = "null"
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    allinvoices = 0
    all_money = 0
    if params.get("bursedetails"):
        for res in params.get("bursedetails"):
            allinvoices += res["invoices"]
            all_money += res["detailmoney"]
        params["all_money"] = all_money
        params["allinvoices"] = allinvoices
    else:
        params["all_money"] = "null"
        params["allinvoices"] = "null"
    sql = ProcessSqlMap.create_bursement.format(**params)
    mysql_tools.execute_sql(sql)
    bursment_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = bursment_id
    if params["bursedetails"]:
        sql_detailsburse = ProcessSqlMap.create_detailsburse
        for res in params["bursedetails"]:
            res["bursment_id"] = bursment_id
            subject_sql = ProcessSqlMap.check_subject_name.format(**res)
            res["subject"] = mysql_tools.select_one(subject_sql)
            res["subject"] = None if isinstance(res["subject"], JsonResponse) else res["subject"].get("name")
            mysql_tools.execute_sql(sql_detailsburse.format(**res), commit=True)
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": bursment_id,
            "processName": "bursement"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, bursment_id, "burse")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_purchase(params):
    '''
    创建采购流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "name": "name",
        "purchasedetails": "purchasedetails"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if key == "attach_id":
    #         continue
    #     if val is None:
    #         return err_response(210)
    # if not params["attach_id"]:
    #     params["attach_id"] = "null"
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    params["leave_time"] = "null"
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    all_money = 0
    if params["purchasedetails"]:
        for res in params["purchasedetails"]:
            invoices = res["invoices"]
            detailmoney = res["detailmoney"]
            subtotal = invoices * detailmoney
            res["subtotal"] = subtotal
            all_money += subtotal
    params["all_money"] = all_money
    sql = ProcessSqlMap.create_purchase.format(**params)
    mysql_tools.execute_sql(sql)
    purchase_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if params["purchasedetails"]:
        sql_detailspurchase = ProcessSqlMap.create_detailspurchase
        for res in params["purchasedetails"]:
            res["purchase_id"] = purchase_id
            mysql_tools.execute_sql(sql_detailspurchase.format(**res), commit=True)
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": purchase_id,
            "processName": "purchase"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, purchase_id, "purchase")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_free_stop(params):
    '''
    创建临时停车流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "license": "license",
        "consume_money": "consume_money",
        "derate_level": "derate_level"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if key == "attach_id":
    #         continue
    #     if val is None:
    #         return err_response(210)
    # if not params["attach_id"]:
    #     params["attach_id"] = "null"
    params["leave_time"] = "null"
    
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    derate_level = mysql_tools.select_one(ProcessSqlMap.check_derate_level.format(**params))
    derate_level = int(derate_level["type_name"][2:-1])
    params["derate_money"] = derate_level / 100 * params["consume_money"]
    sql = ProcessSqlMap.create_free_stop.format(**params)
    mysql_tools.execute_sql(sql)
    stop_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": stop_id,
            "processName": "freeStop"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, stop_id, "freeStop")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_special_car(params):
    '''
    创建特殊车辆流程
    :param params:
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "license": "license",
        "start_time": "start_time",
        "end_time": "end_time",
        "derate_level": "derate_level"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # for key, val in params.items():
    #     if key == "attach_id":
    #         continue
    #     if val is None:
    #         return err_response(210)
    # check_user_name_position = mysql_tools.select_one(ProcessSqlMap.check_user_name_position.format(**params))
    # params["shenuser"] = check_user_name_position.get("real_name")
    # params["shenuserzhi"] = check_user_name_position.get("name")
    # params["next_user_name"] = check_user_name_position.get("user_name")
    params["leave_time"] = "null"
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id["status_id"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    params["process_id"] = process_id
    # sql_reviewed = ProcessSqlMap.create_reviewed_new.format(**params)
    # mysql_tools.execute_sql(sql_reviewed)
    sql = ProcessSqlMap.create_special_car.format(**params)
    mysql_tools.execute_sql(sql)
    car_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": car_id,
            "processName": "specialCar"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, car_id, "specialCar")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def process_list(params):
    '''
    所有流程列表
    :param params:
    :return:
    '''
    dict = {
        "user_id": "user_id"
    }
    page_params = {
        "isall": "isAll",
        "size": "limit",
        "page": "page",
        "query_str": "query_str"
    }
    params = get_params(params, dict, page_params)
    if isinstance(params, JsonResponse):
        return params
    sql = add_filter(ProcessSqlMap.process_list, params, ["query_str"],
                     **ProcessSqlMap.FILTER_PARAMS)
    sql += " order by t1.apply_time desc"
    result = mysql_tools.select_all(sql.format(**params))
    # if params.get("isall") is not True:
    if params.get("isall") != 1:
        if isinstance(result, JsonResponse):
            return result
        result = paging_simple(result, params.get("page"), params.get("size"))
    for res in result:
        res["apply_time"] = str(res["apply_time"])
        if res["status_name"] == "已结束":
            res["step"] = "已通过"
        elif res["status_name"] == "未通过":
            res["step"] = "未通过"
        else:
            if res.get("shenuserzhi"):
                res["step"] = str(res.get("shenuserzhi")) + "审批"
            else:
                res["step"] = "未审批"
        res.pop("shenuserzhi")
    return success_response(result)


def create_overtime(params):
    '''
    创建加班流程
    :param adress:      加班地址
    :param time:        加班时间,小时
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "adress": "adress",
        "leave_time": "leave_time"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    result = mysql_tools.execute_sql(ProcessSqlMap.create_overtime.format(**params))
    if isinstance(result, JsonResponse):
        return result
    overtime_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": overtime_id,
            "processName": "overTime"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, overtime_id, "overTime")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_evection(params):
    '''
    创建出差流程
    :param adress:      出差地址
    :param leave_days:  出差天数
    :param type_id:     交通工具
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "adress": "adress",
        "leave_time": "leave_days",
        "type_id": "type_id"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入evection表
    result = mysql_tools.execute_sql(ProcessSqlMap.create_evection.format(**params))
    if isinstance(result, JsonResponse):
        return result
    evection_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": evection_id,
            "processName": "evection"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, evection_id, "evection")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_regular(params):
    '''
    创建转正流程
    :param dobetter:    做的更好
    :param advice:      建议
    :param experience:  体会心得
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "dobetter": "dobetter",
        "advice": "advice",
        "experience": "experience"
    }
    null_params = {
        "attach_id": "attach_id"
    }
    replace_params = {
        "attach_id": "null"
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入regular表
    result = mysql_tools.execute_sql(ProcessSqlMap.create_regular.format(**params))
    if isinstance(result, JsonResponse):
        return result
    regular_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": regular_id,
            "processName": "regular"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, regular_id, "regular")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_borrow(params):
    '''
    创建借款流程
    :param employ_time:     借款日期
    :param money:           借款金额
    :param money_big:       借款金额大写
    :param pay_type:        借款方式
    :param remark:          描述
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        # "start_time": "start_time",
        # "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "employ_time": "employ_time",
        "money": "money",
        "money_big": "money_big",
        "pay_type": "pay_type",
    }
    null_params = {
        "attach_id": "attach_id",
        "remark": "remark"
    }
    replace_params = {
        "attach_id": "null",
        "remark": None
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入borrow表
    if params.get("remark"):
        result = mysql_tools.execute_sql(ProcessSqlMap.create_borrow.format(**params))
    else:
        result = mysql_tools.execute_sql(ProcessSqlMap.create_borrow_noremark.format(**params))
    if isinstance(result, JsonResponse):
        return result
    borrow_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": borrow_id,
            "processName": "borrow"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, borrow_id, "borrow")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_payment(params):
    '''
    创建付款流程
    :param bank_address:    开户行
    :param bank_num:        银行卡号
    :param finally_time:    截止时间
    :param pay_type:        付款方式
    :param remark:          描述
    :param money:           金额
    :param money_big:       金额大写
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        # "start_time": "start_time",
        # "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "bank_address": "bank_address",
        "bank_num": "bank_num",
        "finally_time": "finally_time",
        "money": "money",
        "money_big": "money_big",
        "pay_type": "pay_type",
    }
    null_params = {
        "attach_id": "attach_id",
        "remark": "remark"
    }
    replace_params = {
        "attach_id": "null",
        "remark": None
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入payment表
    if params.get("remark"):
        result = mysql_tools.execute_sql(ProcessSqlMap.create_payment.format(**params))
    else:
        result = mysql_tools.execute_sql(ProcessSqlMap.create_payment_noremark.format(**params))
    if isinstance(result, JsonResponse):
        return result
    payment_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": payment_id,
            "processName": "payment"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, payment_id, "payment")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_chapter(params):
    '''
    创建公章流程
    :param chapter_type:       用章类型
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "chapter_type": "chapter_type",
    }
    null_params = {
        "attach_id": "attach_id",
    }
    replace_params = {
        "attach_id": "null",
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入chapter表
    result = mysql_tools.execute_sql(ProcessSqlMap.create_chapter.format(**params))
    if isinstance(result, JsonResponse):
        return result
    chapter_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": chapter_id,
            "processName": "chapter"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, chapter_id, "chapter")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_contract(params):
    '''
    创建合同会签
    :param money:       金额
    :param money_big:   大写金额
    :param remark:      备注
    :param customer:    客户
    :param contact:     联系人
    :param contract_num: 联系人电话
    :param contract_name:合同名称
    :param outline:     合同概要
    :return:
    '''
    dict = {
        "process_user_id": "process_user_id",
        # "start_time": "start_time",
        # "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        "money": "money",
        "money_big": "money_big",
        "customer": "customer",
        "contact": "contact",
        "contract_num": "contract_num",
        "contract_name": "contract_name",
        "outline": "outline",
    }
    null_params = {
        "attach_id": "attach_id",
        "remark": "remark"
        
    }
    replace_params = {
        "attach_id": "null",
        "remark": None
        
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.INSERT_PROCESS_NOTIME.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    
    # 插入contract表
    if params.get("remark"):
        result = mysql_tools.execute_sql(ProcessSqlMap.create_contract.format(**params))
    else:
        result = mysql_tools.execute_sql(ProcessSqlMap.create_contract_noremark.format(**params))
    
    if isinstance(result, JsonResponse):
        return result
    contract_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": contract_id,
            "processName": "contract"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, contract_id, "contract")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


def create_special_matter(params):
    '''
    创建特殊事项
    参数全部是process_list表中的,没有额外的表
    :params start_time:     处理日期
    '''
    dict = {
        "process_user_id": "process_user_id",
        "start_time": "start_time",
        # "end_time": "end_time",
        "deeply_id": "deeply_id",
        "process_des": "process_des",
        "process_name": "process_name",
        "config_id": "config_id",
        # "user_id": "next_user_id",
        
    }
    null_params = {
        "attach_id": "attach_id",
        
    }
    replace_params = {
        "attach_id": "null",
        
    }
    params = get_params(params, dict, null_params, **replace_params)
    if isinstance(params, JsonResponse):
        return params
    params["leave_time"] = "null"
    # 插入process_list表
    sql_status_id = ProcessSqlMap.check_status_id.format(**{"status_name": "未处理"})
    status_id = mysql_tools.select_one(sql_status_id)
    params["status_id"] = status_id.get("status_id")
    sql_check_config_name = ProcessSqlMap.check_config_name.format(**params)
    type_name = mysql_tools.select_one(sql_check_config_name)
    params["type_name"] = type_name["type_name"]
    sql_process_list = ProcessSqlMap.create_special_matter.format(**params)
    result = mysql_tools.execute_sql(sql_process_list)
    process_id = mysql_tools.select_one(ProcessSqlMap.GET_ID).get("id")
    if isinstance(result, JsonResponse):
        return result
    params["process_id"] = process_id
    try:
        push_dict = {
            "isStart": True,  # 是否是发起
            "userId": params.get("process_user_id"),
            "processId": process_id,
            "classId": process_id,
            "processName": "specialMatter"
        }
        RabbitPublisher.run("startprocess", push_dict)
        # process.delay(params.get("process_user_id"), process_id, process_id, "specialMatter")
        # message = "收到一个待审批流程"
        # j_push.delay(message, [params.get("next_user_name")])
    except Exception as e:
        print("Error is", e)
    finally:
        return success_response({"process_id": process_id})


@format_response
def index(request):
    return com_views(request, {
        "my_apply": my_apply,
        "my_unreviewed": my_unreviewed,
        "my_reviewed": my_reviewed,
        "check_process": check_process,
        "examine_process": examine_process,
        "create_holiday": create_holiday,
        "create_go_out": create_go_out,
        "create_resign": create_resign,
        "create_bursement": create_bursement,
        "create_purchase": create_purchase,
        "create_free_stop": create_free_stop,
        "create_special_car": create_special_car,
        "create_overtime": create_overtime,
        "create_evection": create_evection,
        "create_regular": create_regular,
        "create_borrow": create_borrow,
        "create_payment": create_payment,
        "create_chapter": create_chapter,
        "create_contract": create_contract,
        "create_special_matter": create_special_matter,
        "process_list": process_list
    })
